/*
 * Copyright (C) 2010-2011 sunjumper@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package mfinder;

import mfinder.annotation.Action;
import mfinder.annotation.Namespace;
import mfinder.annotation.Result;
import mfinder.annotation.Scope;
import mfinder.interceptor.DefaultInterceptorStack;
import mfinder.result.DefaultResult;

/**
 * SimpleAction
 */
@Namespace(name = "/test")
public class SimpleAction {

    /** 表示Action执行成功 */
    public static final String SUCCESS = "success";

    /**
     * 测试简单调用。
     *
     * @return Action执行成功。
     */
    @Action(name = "simple", interceptorStack = DefaultInterceptorStack.SAMPLE_INTERCEPTOR_STACK)
    protected String simple() {
        return SUCCESS;
    }

    @Action(name = "param", interceptorStack = DefaultInterceptorStack.SAMPLE_INTERCEPTOR_STACK,
    scope = Scope.PROTOTYPE)
    public String param(String str, int n) {
        return str + n;
    }

    /**
     * 测试调用时抛出异常。
     *
     * @return 抛出异常则返回null。
     */
    @Action(name = "exception", interceptorStack = DefaultInterceptorStack.SAMPLE_INTERCEPTOR_STACK)
    public String exception() {

        //throw exception
        System.out.println(1 / 0);
        return null;
    }

    /**
     * 测试forward结果类型。
     *
     * @return 跳转至/test/simple都的结果。
     */
    @Action(interceptorStack = DefaultInterceptorStack.DEFAULT_INTERCEPTOR_STACK,
    results = {
        @Result(name = "forward", type = DefaultResult.FORWARD, location = "/test/simple")
    })
    public String forward() {
        return "forward";
    }

    /**
     * 二次forward调用。
     *
     * @return 跳转至/test/fowward -> /test/simple都的结果。
     */
    @Action(interceptorStack = DefaultInterceptorStack.DEFAULT_INTERCEPTOR_STACK,
    results = {
        @Result(name = "forward2", type = DefaultResult.FORWARD, location = "/test/forward")
    })
    public String forward2() {
        return "forward2";
    }

    /**
     *
     * 提供基于返回单表达式字符串的结果视图。
     *
     * 例如：
     * forward : /test/forward
     * redirect: /test
     * jsp: /index.jsp
     * ......
     *
     * @param type 测试输入的返回类型值。
     *
     * @return 结果视图的表达式字符串。
     */
    @Action(interceptorStack = DefaultInterceptorStack.DEFAULT_INTERCEPTOR_STACK,
    results = {
        @Result(name = "test", type = DefaultResult.FORWARD, location = "/test/simple"),
        @Result(name = "*")
    })
    public String autoRender(String type) {
        return type;
    }
}
